<?php
declare (strict_types=1);

namespace app\user\logic\user;

use app\admin\logic\system\Config as SystemConfig;
use app\course\model\record\LearnCourseRecord;
use app\user\model\user\FaceVerify as FaceVerifyModel;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

/**
 * Class FaceVerify
 *
 * @package app\user\logic\user
 */
class FaceVerify extends FaceVerifyModel
{
    /**
     * 获取已经验证的次数
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/12/6
     *
     * @param int    $userId
     * @param string $sceneKey
     * @param int    $sceneType
     * @param int    $sceneId
     * @return int
     */
    public function getVerifyCount(int $userId, string $sceneKey, int $sceneType, int $sceneId)
    {
        return $this->where('user_id', $userId)
            ->where('scene_key', $sceneKey)
            ->where('scene_type', $sceneType)
            ->where('scene_id', $sceneId)
            ->count('id');
    }

    /**
     * 人脸数据
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/12/6
     *
     * @param string   $sceneKey
     * @param int      $sceneType
     * @param int      $sceneId
     * @param int|null $userId
     * @param int|null $mhm_id
     * @return array
     */
    public function getFaceData(string $sceneKey, int $sceneType, int $sceneId, ?int $userId = null, ?int $mhm_id = null)
    {
        $userId = is_null($userId) ? request()->mid : $userId;
        $mhm_id = is_null($mhm_id) ? (request()->mhm_id ?: 1) : $mhm_id;
        // 获取配置的验证次数
        // $config       = SystemConfig::get('face.config', ['verify_number' => 0, 'verify_timespan' => 0]);
        $config       = SystemConfig::get('system.onhook', ['face_max_number' => 0, 'face_time_interval' => 0], $mhm_id, true);
        $verifyNumber = $config['face_max_number'] ?? 0;

        switch ($sceneKey) {
            // 课时
            case 'course_section':
                // 进入观看场景
                if ($sceneType == 2) {
                    // 课时是否已经学完,学完之后不需要在观看课时时验证
                    /** @var learnCourseRecord $learnCourseRecord */
                    $learnCourseRecord = app(learnCourseRecord::class);
                    $finished          = (int)$learnCourseRecord->where('section_id', $sceneId)->where('user_id', $userId)->value('finished');
                    if ($finished) {
                        $verifyNumber = 0;
                    }
                }
                break;
            // 考试
            case 'exam':

                break;
        }

        // 已经验证的次数
        $verifyCount = $this->getVerifyCount($userId, $sceneKey, $sceneType, $sceneId);

        return [
            'verified_count'     => $verifyCount,
            'need_verify_number' => $verifyNumber > $verifyCount ? $verifyNumber - $verifyCount : 0,
            'verify_timespan'    => $config['face_time_interval'] ?? 0
        ];
    }

    /**
     * 获取用户人脸列表
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/12/7
     *
     * @param int    $userId
     * @param string $sceneKey
     * @param int    $sceneId
     * @return FaceVerify[]|array|\think\Collection
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getUserFaceList(int $userId, string $sceneKey, int $sceneId)
    {
        return $this->where('user_id', $userId)
            ->where('scene_key', $sceneKey)
            ->where('scene_id', $sceneId)
            ->with(['attachUrl'])
            ->field(['id', 'attach_id', 'create_time'])
            ->select();
    }
}
